<?php
define('IN_TG','photo_show');
define('IN_JS','photo_show');
define('SCRIPT','photo_show');
session_start();
require('includes/common.inc.php');
require('header.php');
$arr=array();
$type=1;
//分页
$sql_count = "select count(tg_id) from tg_photo where tg_active=1";
$res=_query($sql_count);
if(isset($_GET['id'])) {
	$tgid = intval($_GET['id']);
	$sql = "select tg_name,tg_type,tg_username from tg_dir where tg_id={$tgid} limit 1";
	$reres = _query($sql);
	$dir_row = _fetch_array_list($reres);
	$type=$dir_row['tg_type'];
	if (isset($_COOKIE['username']) && ($dir_row['tg_username'] == $_COOKIE['username']))
	{
		$type=0;
	}
	if(!empty($_POST))
	{
		$sql = "select tg_password from tg_dir where tg_id={$tgid} limit 1";
		$reres = _query($sql);
		$row = _fetch_array_list($reres);
		if($row['tg_password']==sha1($_POST['password']))
		{
			$type=0;
			$_SESSION[$tgid.'pwd']=sha1($_POST['password']);
		}
		else
		{
			_alert_back("密码错误");
		}
	}
	else if(isset($_SESSION[$tgid.'pwd']))
	{
		$sql = "select tg_password from tg_dir where tg_id={$tgid} limit 1";
		$reres = _query($sql);
		$row = _fetch_array_list($reres);
		if($row['tg_password']==$_SESSION[$tgid.'pwd'])
		{
			$type=0;
		}
	}
	$_id = 'id='.$_GET['id'].'&';
	$sql = "select tg_username,tg_id,tg_name,tg_url,tg_readcount,tg_commendcount from tg_photo where tg_sid={$tgid} and tg_active=1";
	$size = 8;
	_page($sql, $size);
	$sql .= " limit $_pagenum,$size";
	$reres = _query($sql);
	while ($row = _fetch_array_list($reres)) {
		$arr[] = $row;
	}
}
if(isset($_GET['action']))
{
	$tgid = intval($_GET['id']);
	$photo_id=intval($_GET['photo_id']);
	if($_GET['action']=='delete') {
		$reres1 = _query("select tg_url,tg_username from tg_photo where tg_id={$photo_id}");
		$row1 = _fetch_array_list($reres1);
		if($_COOKIE['username']==$row1['tg_username'])
		{
			$result=_remove_Dir($row1['tg_url']);
			$reres2 = _query("delete from tg_photo where tg_id={$photo_id}");
			if ($reres2)
			{
				_location('删除成功', "photo_show.php?id={$tgid}");
			}
			else
			{
				_alert_back("请刷新页面重试");
			}
		}
		else
		{
			_alert_back("你没有权限删除该文件");
		}
	}
	else if($_GET['action']=='set_face') {
		$reres1 = _query("select tg_url,tg_username from tg_photo where tg_id={$photo_id}");
		$row1 = _fetch_array_list($reres1);
		if($_COOKIE['username']==$row1['tg_username'])
		{
			$face=$row1['tg_url'];
			$reres2 = _query("update tg_dir set tg_face='{$face}' where tg_id={$tgid}");
			if($reres2)
			{
				header("location:photo_show.php?id=$tgid");
			}
			else
			{
				_alert_back("封面设置失败");
			}
		}
		else
		{
			_alert_back("你没有权限设置封面");
		}
	}
}
?>
	<div id="photo">
		<h2><?php echo $dir_row['tg_name'];?></h2>
		<?php if($type==0):?>
			<?php if(!empty($arr)):?>
				<?php  foreach($arr as $value):?>
					<dl>
						<dt><a href="photo_detail.php?id=<?php echo $value['tg_id']?>"><img src="<?php echo $value['tg_url']?>" width="180" height="120" /></a></dt>
						<dd><a href="photo_detail.php?id=34"><?php echo $value['tg_name']?></a></dd>
						<dd>阅(<strong><?php echo $value['tg_readcount'];?></strong>) 评(<strong><?php echo $value['tg_commendcount'];?></strong>) 上传者：<?php echo $value['tg_username']?></dd>
						<?php if(isset($_COOKIE['username'])&&($value['tg_username']==$_COOKIE['username'])):?>
							<dd>[<a href="photo_show.php?action=set_face&id=<?php echo $_GET['id']?>&photo_id=<?php echo $value['tg_id']?>">设为封面</a>]
								[<a href="photo_show.php?action=delete&id=<?php echo $_GET['id']?>&photo_id=<?php echo $value['tg_id']?>">删除</a>]</dd>
						<?php endif;?>
					</dl>
				<?php  endforeach;?>
				<div id="page_num"><?php _paging(1);?></div>
			<?php else:?>
				<p align="center"><strong>暂无相册信息</strong></p>
			<?php endif;
			if (isset($_COOKIE['username'])):
				?>
			<p><a href="photo_add_img.php?id=<?php echo $_GET['id']?>">上传图片</a></p>
				<p><a href="photo_delete.php?id=<?php echo $_GET['id'] ?>">删除相册</a></p>
			<?php endif;
		else: ?>
			<form method="post" action="photo_show.php?id=<?php echo $tgid ?>&page=<?php echo isset($_GET['page'])?$_GET['page']:1;?>">
				<p>请输入密码：<input type="password" name="password" />
					<input type="submit" value="确认" /></p>
			</form>
		<?php endif;?>
	</div>
<?php
require('includes/footer.inc.php');
?>